package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BaseIndex;
import com.ruoyi.base.service.IBaseIndexService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 首页地址Controller
 *
 * @author zzp
 * @date 2022-09-28
 */
@RestController
@RequestMapping("/base/index")
public class BaseIndexController extends BaseController {
    @Autowired
    private IBaseIndexService baseIndexService;

    /**
     * 查询首页地址列表
     */
    @PreAuthorize("@ss.hasPermi('base:index:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseIndex baseIndex) {
        startPage();
        List<BaseIndex> list = baseIndexService.selectBaseIndexList(baseIndex);
        return getDataTable(list);
    }

    /**
     * 导出首页地址列表
     */
    @PreAuthorize("@ss.hasPermi('base:index:export')")
    @Log(title = "首页地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseIndex baseIndex) {
        List<BaseIndex> list = baseIndexService.selectBaseIndexList(baseIndex);
        ExcelUtil<BaseIndex> util = new ExcelUtil<BaseIndex>(BaseIndex.class);
        util.exportExcel(response, list, "首页地址数据");
    }

    /**
     * 获取首页地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:index:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(baseIndexService.selectBaseIndexById(id));
    }

    /**
     * 新增首页地址
     */
    @PreAuthorize("@ss.hasPermi('base:index:add')")
    @Log(title = "首页地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseIndex baseIndex) {
        if (baseIndex.getIndexType().equals("video")) {
            BaseIndex indexList = baseIndexService.selectBaseIndexUse();
            if (StringUtils.isNotNull(indexList)) {
                return AjaxResult.error("已经存在视频首页，不可添加");
            }
        }
        return toAjax(baseIndexService.insertBaseIndex(baseIndex));
    }

    /**
     * 修改首页地址
     */
    @PreAuthorize("@ss.hasPermi('base:index:edit')")
    @Log(title = "首页地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseIndex baseIndex) {
        return toAjax(baseIndexService.updateBaseIndex(baseIndex));
    }

    /**
     * 删除首页地址
     */
    @PreAuthorize("@ss.hasPermi('base:index:remove')")
    @Log(title = "首页地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseIndexService.deleteBaseIndexByIds(ids));
    }

    /**
     * 获取首页地址
     */
    @GetMapping(value = "/getIndexVideoUrl")
    public AjaxResult getIndexVideoUrl() {
        BaseIndex index = baseIndexService.selectBaseIndexUse();
        if (StringUtils.isNotNull(index)) {
            AjaxResult ajax = AjaxResult.success();
            ajax.put(AjaxResult.DATA_TAG, index.getIndexVideoUrl());
            return ajax;
        } else {
            List<BaseIndex> imgList = baseIndexService.selectBaseIndexImgList();
            if (imgList.size() > 0) {
                AjaxResult ajax = AjaxResult.success();
                ajax.put(AjaxResult.DATA_TAG, null);
                ajax.put("imgList", imgList);
                return ajax;
            } else {
                return AjaxResult.success(null);
            }
        }
    }
}
